package cn.edu.sgu.www.mhxysy.service.baoshi.impl;

import cn.edu.sgu.www.mhxysy.entity.baoshi.EquipmentBaoshiGrade;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.baoshi.EquipmentBaoshiGradeMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.baoshi.EquipmentBaoshiGradeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class EquipmentBaoshiGradeServiceImpl implements EquipmentBaoshiGradeService {

    private final EquipmentBaoshiGradeMapper equipmentBaoshiGradeMapper;

    @Autowired
    public EquipmentBaoshiGradeServiceImpl(EquipmentBaoshiGradeMapper equipmentBaoshiGradeMapper) {
        this.equipmentBaoshiGradeMapper = equipmentBaoshiGradeMapper;
    }

    @Override
    public Integer selectEquipmentGrade(Integer baoshiGrade) {
        QueryWrapper<EquipmentBaoshiGrade> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("equipment_grade");
        queryWrapper.ge("baoshi_grade", baoshiGrade);
        queryWrapper.orderByAsc("equipment_grade");

        List<EquipmentBaoshiGrade> list = equipmentBaoshiGradeMapper.selectList(queryWrapper);

        if (list.isEmpty()) {
            throw new GlobalException(ResponseCode.BAD_REQUEST, "未找到与宝石等级匹配的装备等级。");
        }

        EquipmentBaoshiGrade grade = list.get(0);

        return grade.getEquipmentGrade();
    }

}